package com.vincent.kdp.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.vincent.kdp.R;
import com.vincent.kdp.ui.bean.response.CommodityListOut;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/6/13 0013.
 */
public class CommodityListAdapter extends RecyclerView.Adapter<CommodityListAdapter.ViewHolder> {

    private Context context;
    private OnItemClicker onItemClicker;
    private int itemCount = 0;
    private List<CommodityListOut.DataBean.ListBean> commodityList = new ArrayList<>();

    public CommodityListAdapter(Context context, OnItemClicker onItemClicker) {
        this.context = context;
        this.onItemClicker = onItemClicker;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        CommodityListOut.DataBean.ListBean c = commodityList.get(position);
        Glide.with(context).load("http://" + c.getImage()).placeholder(R.mipmap.ic_commodity_one).into(holder.mCommodityImg);
        holder.mCommodityNameTv.setText(c.getName());
        holder.mPriceTv.setText("￥" + c.getPrice());
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rl_commodity, parent, false);
        return new ViewHolder(view, onItemClicker);
    }

    public void refreshData(List<CommodityListOut.DataBean.ListBean> list) {
        commodityList.clear();
        commodityList = list;
        itemCount = commodityList.size();
    }

    public void addData(List<CommodityListOut.DataBean.ListBean> list) {
        commodityList.addAll(list);
        itemCount = commodityList.size();
    }

    public CommodityListOut.DataBean.ListBean getItem(int index){
        return commodityList.get(index);
    }

    @Override
    public int getItemCount() {
        return itemCount;
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @Bind(R.id.commodity_img)
        ImageView mCommodityImg;
        @Bind(R.id.commodity_name_tv)
        TextView mCommodityNameTv;
        @Bind(R.id.price_tv)
        TextView mPriceTv;

        private OnItemClicker onCheckedItemClicker;

        public ViewHolder(View itemView, OnItemClicker onCheckedItemClicker) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(this);
            this.onCheckedItemClicker = onCheckedItemClicker;
        }

        @Override
        public void onClick(View v) {
            onCheckedItemClicker.operate(v, getAdapterPosition());
        }
    }

    public interface OnItemClicker {
        void operate(View view, int position);
    }


}
